package tmacsoftware.ursql;

import java.util.Vector;

public class UrSQLEntity
{
    // numerical, unique id of entity
    private int id;
    // vector containing all entries in the entity
    private Vector<UrSQLEntry> entries;

    /**
     * Create new entity
     */
    public UrSQLEntity()
    {
        entries = new Vector<UrSQLEntry>();
    }

    /**
     * Create a new entity with the given id
     * @param id ID of new entity
     */
    public UrSQLEntity(int id)
    {
        this();
        this.id = id;
    }

    /**
     * Get the unique numerical id of the entity
     * @return ID of the entity
     */
    public int getId()
    {
        return this.id;
    }

    /**
     * Set the numerical id the entity
     * @param id New id for entity
     * @todo check if new id is unique?
     */
    public void setId(int id)
    {
        this.id = id;
    }

    /**
     * Get all entries in the entity
     * @return Vector of entries
     */
    public Vector<UrSQLEntry> getEntries()
    {
        return getEntriesAsVector();
    }

    /**
     * Get all entries in the entity as a vector
     * @return Vector of entries
     */
    public Vector<UrSQLEntry> getEntriesAsVector()
    {
        return this.entries;
    }

    /**
     * Get all entries in the entity as an array
     * @return Array of entries
     */
    public UrSQLEntry[] getEntriesAsArray()
    {
        UrSQLEntry[] returnArray = new UrSQLEntry[this.entries.size()];
        Vector<UrSQLEntry> allEntries = getEntries();
        for (int i = 0; i < this.entries.size(); i++)
        {
            returnArray[i] = allEntries.get(i);
        }
        return returnArray;
    }

    /**
     * Set all entries of entity
     * @param entries Vector containing entries for entity
     */
    public void setEntries(Vector<UrSQLEntry> entries)
    {
        this.entries = entries;
    }

    /**
     * Add an entry to the entity
     * @param entry Entry to add to entity
     */
    public void addEntry(UrSQLEntry entry)
    {
        entry.setEntity(this);
        // remove entry if it already exists
        for (int i = 0; i < this.entries.size(); i++)
        {
            if (entry.getKey().compareTo(this.entries.get(i).getKey()) == 0)
            {
                this.entries.remove(i);
            }
        }
        // add new entry
        this.entries.add(entry);
    }

    /**
     * Get the entry with the given key
     * @param key Key of entry
     * @return Entry object
     */
    public UrSQLEntry getEntry(String key)
    {
        Vector<UrSQLEntry> entries = getEntriesAsVector();
        UrSQLEntry returnEntry = new UrSQLEntry();
        for (int i = 0; i < entries.size(); i++)
        {
            if (key.compareTo(entries.get(i).getKey()) == 0)
            {
                returnEntry = new UrSQLEntry(entries.get(i).getKey(), entries.get(i).getValue());
                returnEntry.setEntity(this);
            }
        }
        return returnEntry;
    }

    /**
     * Get the total number of entries in the entity
     * @return Number of entries in the entity
     */
    public int countEntries()
    {
        Vector<UrSQLEntry> allEntries = getEntriesAsVector();
        return allEntries.size();
    }

    /**
     * Remove the given entry from entity
     * @param removeEntry Entry to remove
     */
    public void removeEntry(UrSQLEntry removeEntry)
    {
        // get all entries
        Vector<UrSQLEntry> allEntries = this.getEntries();
        // iterate through entries
        for (int i = 0; i < allEntries.size(); i++)
        {
            // remove entry from vector if keys match
            if (allEntries.get(i).getKey().compareTo(removeEntry.getKey()) == 0)
            {
                allEntries.remove(i);
            }
        }
        // set entries to vector with approprate entry removed
        this.setEntries(allEntries);
    }

    /**
     * Remove all entries from entity
     */
    public void removeAllEntries()
    {
        Vector<UrSQLEntry> blank = new Vector<UrSQLEntry>();
        this.setEntries(blank);
    }
}
